# -*- coding: utf-8 -*-

#
# SConstruct
# This file is part of OCI (Open Chicken Invaders)
#
# Copyright (C) 2010-2014 - Aleksey Konovalov (konovalov.aleks@gmail.com)
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU Library General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU Library General Public License for more details.
#
#  You should have received a copy of the GNU Library General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

import fnmatch
import os

def FindFiles(directory, mask):
    files = []
    for root, dirnames, filenames in os.walk(directory):
        for filename in fnmatch.filter(filenames, mask):
            if not filename.startswith('_test_'):
                files.append(os.path.join(root, filename))
    return files

TARGET_FILE_NAME = 'chickeninvaders'
TARGET_FILE_EXT = ''

AddOption('--use_sfml',
          dest='use_sfml',
          action='store_true',
          help='use sfml instead sdl')

USE_SFML = GetOption('use_sfml')

AddOption('--c++11',
          dest='c++11',
          action='store_true',
          help='use c++11')

USE_CPP11 = GetOption('c++11')

env = Environment()
env['ENV']['TERM'] = os.environ['TERM']
if 'CXX' in os.environ:
    env['CXX'] = os.environ['CXX']
if 'CCFLAGS' in os.environ:
    env['CCFLAGS'] = os.environ['CCFLAGS']

if not USE_SFML:
    env['CCFLAGS'] += [ '`sdl2-config --cflags`' ]

libs = ['tinyxml', 'boost_chrono', 'boost_system', 'boost_thread']

if USE_SFML:
    libs += ['sfml-graphics', 'sfml-window', 'sfml-system', 'sfml-audio']
else:
    env['LINKFLAGS'] += ['`sdl2-config --libs`']
    libs += ['SDL2', 'SDL2_mixer']

if 'mingw' in env['CXX']:
    libs += ['glu32', 'glew32', 'opengl32', 'gdi32', 'winmm', 'OpenAL32.dll',
             'sndfile']
    TARGET_FILE_EXT = '.exe'

# Используем кэш, чтобы Scons не разбирал исходники для определения зависимостей
# при каждой сборке
#env.SetOption('implicit_cache', 1)

AddOption('--mode',
          dest='mode',
          type='string',
          nargs=1,
          help='build mode',
          default='release')

modecfg = {
            'release': {
                'flags': ['-O3'],
                'defines': ['NDEBUG']
            },
            'debug': {
                'flags': ['-g', '-O0'],
                'defines': ['DEBUG']
            }
        }.get(GetOption('mode'))

if not modecfg:
    raise Exception('Unknown build mode: "%s"' % GetOption('mode'))

env.Append(CCFLAGS=['-Wextra', '-I.', '-DUSE_BOOST_CHRONO',
                    '-DBOOST_SP_DISABLE_THREADS', '-DTIXML_USE_STL',
                    modecfg['flags']])
if USE_CPP11:
    env.Append(CCFLAGS=['--std=c++11', '-DCPP11'])
if USE_SFML:
    env.Append(CCFLAGS=['-DUSE_SFML'])
env.Append(CPPDEFINES=[modecfg['defines']])
env.Program(target=TARGET_FILE_NAME + TARGET_FILE_EXT,
            source=FindFiles('.', '*.cpp'),
            LIBS=libs, CPPPath='.')
